package com.examstack.portal.controller.page;

import com.examstack.common.domain.question.*;
import com.examstack.common.util.Page;
import com.examstack.common.util.PagingUtil;
import com.examstack.common.util.QuestionAdapter;
import com.examstack.portal.security.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.examstack.portal.service.QuestionService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class QuestionPage {

    @Autowired
    private QuestionService questionService;
    /**
     * 试题上传页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/student/question/question-add", method = RequestMethod.GET)
    public String questionAddPage(Model model) {
        List<Field> fieldList = questionService.getAllField(null);
        model.addAttribute("fieldList", fieldList);
        return "question-add";
    }

    @RequestMapping(value = "/student/question/question-list", method = RequestMethod.GET)
    public String questionListPage(Model model) {

        return "redirect:question-list/filter-0-0-0-0-0-0-1.html";
    }

    /**
     * 试题管理列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/student/question/question-list/filter-{fieldId}-{knowledge}-{questionType}-{tag}-{status}-{searchParam}-{page}.html", method = RequestMethod.GET)
    public String questionListFilterPage(Model model,
                                         @PathVariable("fieldId") int fieldId,
                                         @PathVariable("knowledge") int knowledge,
                                         @PathVariable("questionType") int questionType,
                                         @PathVariable("tag") int tag, @PathVariable("status") int status,
                                         @PathVariable("searchParam") String searchParam,
                                         @PathVariable("page") int page) {

        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = userInfo.getUsername();
        QuestionFilter qf = new QuestionFilter();
        qf.setFieldId(fieldId);
        qf.setKnowledge(knowledge);
        qf.setQuestionType(questionType);
        qf.setTag(tag);
        qf.setStatus(status);
        qf.setCreator(username);

        if (searchParam.equals("0"))
            searchParam = "-1";
        qf.setSearchParam(searchParam);

        Page<Question> pageModel = new Page<Question>();
        pageModel.setPageNo(page);
        pageModel.setPageSize(20);

        //TODO 查找questionlist的时候需要tag
        List<Question> questionList = questionService.getQuestionList(
                pageModel, qf);

        String pageStr = PagingUtil.getPageBtnlinkWithTotal(page,
                pageModel.getTotalPage(),pageModel.getTotalRecord());

        List<Field> fieldList = questionService.getAllField(null);
        model.addAttribute("fieldList", fieldList);

		/*if(fieldList.size() > 0)
			fieldId = fieldList.get(0).getFieldId();*/
        model.addAttribute("knowledgeList",
                questionService.getKnowledgePoint(fieldId,null));

        model.addAttribute("questionTypeList",
                questionService.getQuestionTypeList());

        model.addAttribute("questionFilter", qf);
        model.addAttribute("questionList", questionList);

        model.addAttribute("pageStr", pageStr);
        model.addAttribute("tagList", questionService.getTags(null));
        //保存筛选信息，删除后跳转页面时使用
        model.addAttribute("fieldId", fieldId);
        model.addAttribute("knowledge", knowledge);
        model.addAttribute("questionType", questionType);
        model.addAttribute("searchParam", "-1".equals(searchParam)?"":searchParam);

        //新增试题状态查询条件
        Status statusObject=new Status();
        List<Status> statusList=statusObject.getStatusList();
        model.addAttribute("statusList", statusList);

        return "question-list";
    }

    /**
     * 试题预览页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/student/question/question-preview/{questionId}", method = RequestMethod.GET)
    public String questionPreviewPage(Model model,
                                      @PathVariable("questionId") int questionId, HttpServletRequest request){
        String strUrl = "http://" + request.getServerName() //服务器地址
                + ":"
                + request.getServerPort() + "/";
        Question question = questionService.getQuestionByQuestionId(questionId);
        List<Integer> idList = new ArrayList<Integer>();
        idList.add(questionId);
        List<QuestionQueryResult> questionQueryList = questionService.getQuestionDescribeListByIdList(idList);
        HashMap<Integer, QuestionQueryResult> questionMap = new HashMap<Integer, QuestionQueryResult>();
        for (QuestionQueryResult qqr : questionQueryList) {
            if (questionMap.containsKey(qqr.getQuestionId())) {
                QuestionQueryResult a = questionMap.get(qqr.getQuestionId());
                questionMap.put(qqr.getQuestionId(), a);
            } else {
                questionMap.put(qqr.getQuestionId(), qqr);
            }
        }
        QuestionAdapter adapter = new QuestionAdapter(question,null,questionMap.get(questionId),strUrl);
        String strHtml = adapter.getStringFromXML(true, false, true);
        model.addAttribute("strHtml", strHtml);
        model.addAttribute("question", question);
        return "question-preview";
    }
    /**
     * 移动端试题添加
     */
    @RequestMapping(value = "/student/mobile/mobile-question-add",method = RequestMethod.GET)
    public String mobileQuestionAdd(Model model){
        List<Field> fieldList = questionService.getAllField(null);
        model.addAttribute("fieldList", fieldList);
        return "mobile-question-add";
    }
    @RequestMapping(value = "/student/mobile/mobile-question-list", method = RequestMethod.GET)
    public String mobileQuestionListPage(Model model) {

        return "redirect:mobile-question-list/filter-0-0-0-0-0-0-1.html";
    }
    /**
     * 移动端试题管理列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/student/mobile/mobile-question-list/filter-{fieldId}-{knowledge}-{questionType}-{tag}-{status}-{searchParam}-{page}.html", method = RequestMethod.GET)
    public String mobileQuestionListFilterPage(Model model,
                                         @PathVariable("fieldId") int fieldId,
                                         @PathVariable("knowledge") int knowledge,
                                         @PathVariable("questionType") int questionType,
                                         @PathVariable("tag") int tag, @PathVariable("status") int status,
                                         @PathVariable("searchParam") String searchParam,
                                         @PathVariable("page") int page) {

        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = userInfo.getUsername();
        QuestionFilter qf = new QuestionFilter();
        qf.setFieldId(fieldId);
        qf.setKnowledge(knowledge);
        qf.setQuestionType(questionType);
        qf.setTag(tag);
        qf.setStatus(status);
        qf.setCreator(username);

        if (searchParam.equals("0"))
            searchParam = "-1";
        qf.setSearchParam(searchParam);

        Page<Question> pageModel = new Page<Question>();
        pageModel.setPageNo(page);
        pageModel.setPageSize(20);

        //TODO 查找questionlist的时候需要tag
        List<Question> questionList = questionService.getQuestionList(
                pageModel, qf);

        String pageStr = PagingUtil.getPageBtnlinkWithTotal(page,
                pageModel.getTotalPage(),pageModel.getTotalRecord());

        List<Field> fieldList = questionService.getAllField(null);
        model.addAttribute("fieldList", fieldList);

		/*if(fieldList.size() > 0)
			fieldId = fieldList.get(0).getFieldId();*/
        model.addAttribute("knowledgeList",
                questionService.getKnowledgePoint(fieldId,null));

        model.addAttribute("questionTypeList",
                questionService.getQuestionTypeList());

        model.addAttribute("questionFilter", qf);
        model.addAttribute("questionList", questionList);

        model.addAttribute("pageStr", pageStr);
        model.addAttribute("tagList", questionService.getTags(null));
        //保存筛选信息，删除后跳转页面时使用
        model.addAttribute("fieldId", fieldId);
        model.addAttribute("knowledge", knowledge);
        model.addAttribute("questionType", questionType);
        model.addAttribute("searchParam", "-1".equals(searchParam)?"":searchParam);

        //新增试题状态查询条件
        Status statusObject=new Status();
        List<Status> statusList=statusObject.getStatusList();
        model.addAttribute("statusList", statusList);

        return "mobile-question-list";
    }
    /**
     * 移动端试题预览页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/student/mobile/mobile-question-preview/{questionId}", method = RequestMethod.GET)
    public String mobileQuestionPreviewPage(Model model,
                                      @PathVariable("questionId") int questionId, HttpServletRequest request){
        String strUrl = "http://" + request.getServerName() //服务器地址
                + ":"
                + request.getServerPort() + "/";
        Question question = questionService.getQuestionByQuestionId(questionId);
        List<Integer> idList = new ArrayList<Integer>();
        idList.add(questionId);
        List<QuestionQueryResult> questionQueryList = questionService.getQuestionDescribeListByIdList(idList);
        HashMap<Integer, QuestionQueryResult> questionMap = new HashMap<Integer, QuestionQueryResult>();
        for (QuestionQueryResult qqr : questionQueryList) {
            if (questionMap.containsKey(qqr.getQuestionId())) {
                QuestionQueryResult a = questionMap.get(qqr.getQuestionId());
                questionMap.put(qqr.getQuestionId(), a);
            } else {
                questionMap.put(qqr.getQuestionId(), qqr);
            }
        }
        QuestionAdapter adapter = new QuestionAdapter(question,null,questionMap.get(questionId),strUrl);
        String strHtml = adapter.getStringFromXML(true, false, true);
        model.addAttribute("strHtml", strHtml);
        model.addAttribute("question", question);
        return "mobile-question-preview";
    }
}
